package com.xz.group12306.business.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.xz.group12306.business.common.DBUtils;
import com.xz.group12306.business.common.ServiceException;
import com.xz.group12306.business.dao.ManagerDao;
import com.xz.group12306.business.dao.impl.ManagerDaoImpl;
import com.xz.group12306.business.service.ManagerService;
import com.xz.group12306.domain.Ticket;
import com.xz.group12306.domain.User;

public class ManagerServiceImpl implements ManagerService{

	
	/**
	 * 通过用户信息查询用户 张爱文
	 */
	@Override
	public User findUser(User one) {
		User user = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			user = managerDao.findUser(one);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
			// throw new ServiceException("查询用户错误", e);
			System.out.println("用户查询错误");
		} finally {
			DBUtils.closeConnection(conn);
		}
		return user;
	}

	/**
	 * 顾赛男 根据用户信息查询用户
	 */
	@Override
	public List<User> findUserList(User one, int page, int rows) {
		List<User> users = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			users = managerDao.findUserList(one, page, rows);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
		} finally {
			DBUtils.closeConnection(conn);
		}
		return users;
	}

	/**
	 * 赛男 不知道这是什么东西
	 */
	@Override
	public List<User> getUserList(int pageSize, int start, User one) {
		// TODO Auto-generated method stub
		Connection conn = null;
		List<User> res = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			res = managerDao.getUserList(pageSize, start, one);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);

		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			DBUtils.closeConnection(conn);
		}
		return res;
	}

	/**
	 * 根据用户信息查询单条用户开路 修改更新数据需要用到的方法,但不是更新方法 张爱文
	 */
	@Override
	public User findUserService(User one) {
		User user = new User();
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			user = managerDao.findUser(one);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			DBUtils.rollback(conn);

			System.out.println("查询出错了");
		} finally {
			DBUtils.closeConnection(conn);
		}
		return user;
	}

	/**
	 * 张爱文 对查询到的用户信息进行修改
	 */
	@Override
	public List<User> findUserListUpdate(User one) {
		List<User> users = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			managerDao.updateUser(one);
			System.out.println("到这了");
			users = managerDao.findUserLists(one);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			System.out.println("chucuole");
			DBUtils.rollback(conn);
			
		} finally {
			DBUtils.closeConnection(conn);
		}
		return users;
	}

	/**
	 * 赛男 删除
	 */
	@Override
	public void delUser(int UserID) {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao manageDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			manageDao.delUser(UserID);
			DBUtils.commit(conn);
		} catch (Exception e) {
			DBUtils.rollback(conn);
			System.out.println("删除用户错误");
		} finally {
			DBUtils.closeConnection(conn);
		}
	}

	/**
	 * 查询到所有的用户信息(删除后的) 张爱文
	 */
	@Override
	public List<User> findAllList() {
		List<User> users = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			users = managerDao.findAll();
			DBUtils.commit(conn);
		} finally {
			DBUtils.closeConnection(conn);
		}
		return users;
	}
	

	

	/**
	 * 增加用户记录 顾赛男
	 */
	@Override
	public void addUser(User user) {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao manageDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			manageDao.addUser(user);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			DBUtils.closeConnection(conn);
		}
	}

	/**
	 * 查询车票信息 顾赛男 整合
	 */
	@Override
	public List<Ticket> findTicketList(Ticket ticket, int page, int rows) {
		List<Ticket> tickets = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao manageDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			tickets = manageDao.findTicketList(ticket, page, rows);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			DBUtils.closeConnection(conn);
		}
		return tickets;
	}

	/**
	 * 管理员增加车票信息 张爱文
	 */
	@Override
	public void addTicket(Ticket ticket) {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			managerDao.addTicket(ticket);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("nos");
		} finally {
			DBUtils.closeConnection(conn);
		}
	}

	/**
	 * 查询到所有的车票信息信息(操作后的) 张爱文
	 * 
	 * @throws SQLException
	 */
	@Override
	public List<Ticket> findAllTicket() {
		List<Ticket> tickets = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			tickets = managerDao.findAllTicket();
			DBUtils.commit(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeConnection(conn);
		}
		return tickets;
	}

	/**
	 * 顾赛男 删除车票信息
	 */
	@Override
	public void delTicket(int TicketID) {
		// TODO Auto-generated method stub
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			managerDao.delTicket(TicketID);
			;
			DBUtils.commit(conn);
		} catch (Exception e) {
			DBUtils.rollback(conn);
			try {
				throw new ServiceException("删除车票错误", e);
			} catch (ServiceException e1) {
				e1.printStackTrace();
			}
		} finally {
			DBUtils.closeConnection(conn);
		}
	}

	/**
	 * 根据车票id查询车票信息
	 */
	@Override
	public Ticket findTicket(int TicketID) {
		Ticket ticket = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			ticket = managerDao.findTicket(TicketID);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
			try {
				throw new ServiceException("查询用户错误", e);
			} catch (ServiceException e1) {
				e1.printStackTrace();
			}
		} finally {
			DBUtils.closeConnection(conn);
		}
		return ticket;
	}

	/**
	 * 修改车票信息 顾赛男
	 */
	@Override
	public void updateTicket(Ticket ticket) {
		// TODO Auto-generated method stub
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ManagerDao managerDao = new ManagerDaoImpl(conn);
			DBUtils.beginTransaction(conn);
			managerDao.updateTicket(ticket);
			;
			DBUtils.commit(conn);
		} catch (Exception e) {
			DBUtils.rollback(conn);
			try {
				throw new ServiceException("修改车票错误", e);
			} catch (ServiceException e1) {
				e1.printStackTrace();
			}
		} finally {
			DBUtils.closeConnection(conn);
		}
	}
	
	@Override
	public List<User> findAllUsers() {
		// TODO Auto-generated method stub
		 List<User> users=null;
			Connection conn = null;
			try {
				conn = DBUtils.getConnection();
				ManagerDao managerDao = new ManagerDaoImpl(conn);
				DBUtils.beginTransaction(conn);
		users=managerDao.findAllUsers();
				DBUtils.commit(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				DBUtils.rollback(conn);
				try {
					throw new ServiceException("查询所有用户错误", e);
				} catch (ServiceException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} finally {
				DBUtils.closeConnection(conn);
			}
			return users;
	}


}
